<?php

/*
 * @author Mirza Adiprdahana
 * @copyright 2013 Mirza Adipradhana
 * @version ExecuteScript 2.0
 * 
 * JSON Output Support
 */

/* =========== *//* =========== *//* =========== *//* =========== *//* =========== *//* =========== */
/*                                      D E S K R I P S I                                           */
/* =========== *//* =========== *//* =========== *//* =========== *//* =========== *//* =========== */
/*
 * 
 * ERROR HANDLING:
 * (1) Jika tidak ada input data (input kosong), maka tampilkan error message
 */
/* =========== *//* =========== *//* =========== *//* =========== *//* =========== *//* =========== */


/* =========== *//* =========== *//* =========== *//* =========== *//* =========== *//* =========== */
/*               D E K L A R A S I / I N I S I A L I S A S I    V A R I A B E L                     */
/* =========== *//* =========== *//* =========== *//* =========== *//* =========== *//* =========== */
////
// STANDAR BAKU
$mApplication = new Application();
$mValidation = new InputValidation();
$mFunction = new Fungsi();
$mJSONRep = new JSONDataRepresentation();
$mJSONRep->SetConfigObjectShowed(false);

$_ERRHANDLER = false; //Error Handler, output: Array (ERROR_SECTION, ERROR_CODE)
$_EXECSTATE = true; //Output State: dimana jika bernilai true maka tampilkan output, jika false maka jangan tampilkan output
$_REFVALUE = false; // Nilai referensi yang WAJIB ada, jika tidak maka output false

$REFURL = $mApplication->GetRefererUrl();
$CURURL = $mApplication->GetCurrentUrl();
////
////
// TAMBAHAN
$mValidation->SetInput($_GET['act']);
$mValidation->SetPattern(pAct);
$mAct = $mValidation->CheckValidation();
////

/* =========== *//* =========== *//* =========== *//* =========== *//* =========== *//* =========== */



/* =========== *//* =========== *//* =========== *//* =========== *//* =========== *//* =========== */
/*                              N I L A I   R E F E R E N C E                                       */
/* =========== *//* =========== *//* =========== *//* =========== *//* =========== *//* =========== */
// Nilai reference adalah nilai yang digunakan sebagai acuan untuk output pada halaman
// Nilai ini bersifat mutlak, jika nilai tidak dapat didefinisikan maka output harus gagal
// Sistem hanya menerima request method POST, kecuali GET dengan HTTP REFERER yang dibatasi 
//GET: Proses eksekusi HANYA menerima request method POST

if ($mAct) {
    if ($_SERVER['REQUEST_METHOD'] == "POST") {

        //DEKLARASI VAR POST DISINI
    } else if ($_SERVER['REQUEST_METHOD'] == "GET") {
        //$tFoundRef = array_search($REFURL, $tAllowedURLRef_GET);
        $tFoundRef = true;
        if ($tFoundRef === false) {
            $_EXECSTATE = false;
            $_ERRHANDLER[ERROR_SECTION] = "APPLICATION";
            $_ERRHANDLER[ERROR_CODE] = "HTTP_REFERER_DENIED";
        } else {
            //DEKLARASI VAR GET DISINI
            if ($mAct == 'add') {
                $tPersonKategori = 3101;
                $tAgama = 1;

                $mValidation->SetInput($_GET['idDonatur']);
                $mValidation->SetPattern(pUnsignedInt);
                $tDonaturId = $mValidation->CheckValidation();

                if (empty($_GET['tanggalDaftar']) || !isset($_GET['tanggalDaftar'])) {
                    $_GET['tanggalDaftar'] = date("Y") . "-" . date("m") . "-" . date("d");
                }
                if (empty($tDonaturId)) {
                    //GENERATE PERSON ID
                    $mDonatur = new AmalDonatur();
                    $tPersonId = $mDonatur->GeneratePersonId("31");
                    //GENERATE DONATUR ID
                    $tDonaturId = $mDonatur->GenerateDonaturId();
                    $_REFVALUE = $tPersonId;
                }
            } else {
                $mValidation->SetInput($_GET['idPerson']);
                $mValidation->SetPattern(pPersonId);
                $tPersonId = $mValidation->CheckValidation();

                $mValidation->SetInput($_GET['idDonatur']);
                $mValidation->SetPattern(pDonaturId);
                $tDonaturId = $mValidation->CheckValidation();
                if ($tPersonId) {
                    $_REFVALUE = $tPersonId;

                    if (!$tDonaturId) {
                        $mDonatur = new AmalDonatur();
                        $tDonaturId = $mDonatur->GetDonaturIdByPersonId($tPersonId);
                    }
                } else if ($tDonaturId) {
                    $_REFVALUE = $tDonaturId;
                    if (!$tPersonId) {
                        $mDonatur = new AmalDonatur();
                        $tPersonId = $mDonatur->GetPersonIdByDonaturId($tDonaturId);
                    }
                }
            }
        }
    } else {
        $_EXECSTATE = false;
        $_ERRHANDLER[ERROR_SECTION] = "APPLICATION";
        $_ERRHANDLER[ERROR_CODE] = "REQUEST_METHOD_UNKNOWN";
    }
} else {
    // Execute Method (act) tidak diketahui / tidak valid
    $_EXECSTATE = false;
    $_ERRHANDLER[ERROR_SECTION] = "APPLICATION";
    $_ERRHANDLER[ERROR_CODE] = "EXECUTE_METHOD_UNKNOWN";
}
/* =========== *//* =========== *//* =========== *//* =========== *//* =========== *//* =========== */



/* =========== *//* =========== *//* =========== *//* =========== *//* =========== *//* =========== */
/*                          I N I S I A L I S A S I   D A T A                                        */
/* =========== *//* =========== *//* =========== *//* =========== *//* =========== *//* =========== */
// Data / Nilai pendukung
// Proses ini dilakukan JIKA REFERENCE VALUE VALID
if ($_REFVALUE !== false) {
    //REDIRECTING manual, jika perlu
    $mRedirect = new Redirector("main.php?menu=21&submenu=233");
    $mRedirect->SetId($_REFVALUE);
}

/* =========== *//* =========== *//* =========== *//* =========== *//* =========== *//* =========== */


/* =========== *//* =========== *//* =========== *//* =========== *//* =========== *//* =========== */
/*                           K E T E N T U A N   O U T P U T                                        */
/* =========== *//* =========== *//* =========== *//* =========== *//* =========== *//* =========== */
if ($_REFVALUE) {
    //JIKA ADA KETENTUAN TAMBAHAN
    if ($mAct == 'add') {
        if ($_GET['kategori'] == "") {
            $_EXECSTATE = false;
            $_ERRHANDLER[ERROR_SECTION] = "APPLICATION";
            $_ERRHANDLER[ERROR_CODE] = "REFERENCE_VALUE_NOT_VALID";
        }

        $tIsAutoRedirect = false;
    } else if ($mAct == 'edt') {
        $tIsAutoRedirect = false;
    } else if ($mAct == 'del') {
        $tIsAutoRedirect = false;
    }
} else {
    $_EXECSTATE = false;
    $_ERRHANDLER[ERROR_SECTION] = "APPLICATION";
    $_ERRHANDLER[ERROR_CODE] = "REFERENCE_VALUE_NOT_VALID";
}


/* =========== *//* =========== *//* =========== *//* =========== *//* =========== *//* =========== */
if ($_EXECSTATE) {

    /*        ///////  ///    ///  ///////    //////            */
    /*        ///       /// ///    ///       ///                */
    /*        //////      ///      //////   ///                 */
    /*        ///       /// ///    ///       ///                */
    /*        ///////  ///    ///  ///////    //////            */

    /* =========== *//* =========== *//* =========== *//* =========== *//* =========== *//* =========== */
    /*                    I N I S I L A I S A S I   E X E C                          */
    /* =========== *//* =========== *//* =========== *//* =========== *//* =========== *//* =========== */

    // P E R S O N
    //////////
    $mDataPerson = new DBQueryExecute(false);

    $mDataPerson->SetTableName("app_person");
    $mDataPerson->SetAutoRedirect($tIsAutoRedirect);

    //REFERENCE VALUE
    $mDataPerson->SetPKValue($_REFVALUE);


    // D O N A T U R
    //////////
    $mDataDonatur = new DBQueryExecute(false);

    $mDataDonatur->SetTableName("sra_person");
    $mDataDonatur->SetAutoRedirect($tIsAutoRedirect);

    //REFERENCE VALUE
    $mDataDonatur->SetPKValue($tDonaturId);

    /* =========== *//* =========== *//* =========== *//* =========== *//* =========== *//* =========== */




    /* =========== *//* =========== *//* =========== *//* =========== *//* =========== *//* =========== */
    /*                              S E T U P   E K S E K U S I   D A T A                               */
    /* =========== *//* =========== *//* =========== *//* =========== *//* =========== *//* =========== */
    if ($mAct == 'add') {
        //INSERT PERSON
        //KONVERSI TANGGAL
        //$mFunction->ConvertDateToDBFormat("fTanggalLahir", "DD/MM/YYYY", $mIsMultiForm);
        //PREPARE SINGLE VAR to MULTIFORM VAR
        $mFunction->SetVariableToMultiFormVariableSupport("idPerson", $tPersonId, true, $mIsMultiForm);
        $mFunction->SetVariableToMultiFormVariableSupport("personKategori", $tPersonKategori, true, $mIsMultiForm);
        $mFunction->SetVariableToMultiFormVariableSupport("agama", $tAgama, true, $mIsMultiForm);

        $mDataPerson->SetPrimaryKey('id_person', 'idPerson', pPersonId);
        $mDataPerson->SetField('nama_lengkap', 'nama', pName, "get");
        $mDataPerson->SetField('email', 'email', pMail, "get");
        $mDataPerson->SetField('agama_id', 'agama', pUnsignedInt);
        $mDataPerson->SetField('kelamin_id', 'kelamin', pUnsignedInt, "get");
        $mDataPerson->SetField('goldarah_id', 'darah', pUnsignedInt, "get");
        $mDataPerson->SetField('kota_lahir_id', 'tempatLahir', pUnsignedInt, "get");
        $mDataPerson->SetField('tanggal_lahir', 'tanggalLahir', pDateDB, "get");
        $mDataPerson->SetField('strata_pendidikan_id', 'pendidikan', pUnsignedInt, "get");
        $mDataPerson->SetField('kategori_id', 'personKategori', pUnsignedInt);


        if (!$mDataPerson->GetError()) {
            $tResult = $mDataPerson->ExecuteQuery($mAct);
            $_ERRHANDLER = $mDataPerson->GetError();

            if ($tResult) {
                //INSERT PEGAWAI
                //echo "EXECUTE PERSON....ok";
                $mFunction->SetVariableToMultiFormVariableSupport("idDonatur", $tDonaturId, true, $mIsMultiForm);

                $mDataDonatur->SetPrimaryKey('id_donatur', 'idDonatur', pDonaturId);
                $mDataDonatur->SetField('person_id', 'idPerson', pPersonId);
                $mDataDonatur->SetField('publikasi', 'publikasi', pName, "get");
                $mDataDonatur->SetField('kategori_id', 'kategori', pUnsignedInt, "get");
                $mDataDonatur->SetField('kategori_ket', 'kategoriKet', pLongText, "get");
                $mDataDonatur->SetField('pekerjaan_id', 'pekerjaan', pUnsignedInt, "get");
                $mDataDonatur->SetField('pekerjaan_ket', 'pekerjaanKet', pLongText, "get");
                $mDataDonatur->SetField('media_id', 'media', pUnsignedInt, "get");
                $mDataDonatur->SetField('media_ket', 'mediaKet', pLongText, "get");
                $mDataDonatur->SetField('catatan', 'catatan', pLongText, "get");
                $mDataDonatur->SetField('tanggal_daftar', 'tanggalDaftar', pDateDB, "get");
                if (!$mDataDonatur->GetError()) {
                    //echo "EXECUTE PEGAWAI....ok";
                    $tResult = $mDataDonatur->ExecuteQuery($mAct, false);
                    $_ERRHANDLER = $mDataDonatur->GetError();
                    if ($tResult) {
                        $mDataDonatur->Commit();
                        $mJSONRep->CreateNewData();
                        $mJSONRep->InsertData("Process", "Success");
                        $mJSONRep->CreateNewData();
                        $mJSONRep->InsertData("idPerson", $tPersonId);
                        $mJSONRep->CreateNewData();
                        $mJSONRep->InsertData("idDonatur", $tDonaturId);
                    } else {
                        //INSERT PEGAWAI GAGAL
                        $mJSONRep->SetErrorObjectShowed(true);
                        $mJSONRep->SetErrorSection($_ERRHANDLER[ERROR_SECTION]);
                        $mJSONRep->SetErrorCode($_ERRHANDLER[ERROR_CODE]);
                        $mJSONRep->CreateNewData();
                        $mJSONRep->InsertData("Process", "Failed");
                        $mJSONRep->SetErrorMessage("Create data donatur gagal.");
                    }
                }
            } else {
                //INSERT PEGAWAI GAGAL
                $mJSONRep->SetErrorObjectShowed(true);
                $mJSONRep->SetErrorSection($_ERRHANDLER[ERROR_SECTION]);
                $mJSONRep->SetErrorCode($_ERRHANDLER[ERROR_CODE]);
                $mJSONRep->SetErrorMessage("Ditemukan data yang identik dengan data yang diinputkan.");
            }
        }
    } else if ($mAct == 'edt') {
        $mFunction->SetVariableToMultiFormVariableSupport("idPerson", $tPersonId, true, $mIsMultiForm);
        $mFunction->SetVariableToMultiFormVariableSupport("fCurrentPKValue", $tPersonId, true, $mIsMultiForm);
        $mFunction->SetVariableToMultiFormVariableSupport("personKategori", $tPersonKategori, true, $mIsMultiForm);
        $mFunction->SetVariableToMultiFormVariableSupport("agama", $tAgama, true, $mIsMultiForm);

        $mDataPerson->SetPrimaryKey('id_person', 'fCurrentPKValue', pPersonId);
        $mDataPerson->SetField('nama_lengkap', 'nama', pName, "get");
        $mDataPerson->SetField('email', 'email', pMail, "get");
        $mDataPerson->SetField('agama_id', 'agama', pUnsignedInt);
        $mDataPerson->SetField('kelamin_id', 'kelamin', pUnsignedInt, "get");
        $mDataPerson->SetField('goldarah_id', 'darah', pUnsignedInt, "get");
        $mDataPerson->SetField('kota_lahir_id', 'tempatLahir', pUnsignedInt, "get");
        $mDataPerson->SetField('tanggal_lahir', 'tanggalLahir', pDateDB, "get");
        $mDataPerson->SetField('strata_pendidikan_id', 'pendidikan', pUnsignedInt, "get");
        if (!$mDataPerson->GetError()) {
            $tResult = $mDataPerson->ExecuteQuery($mAct);
            $_ERRHANDLER = $mDataPerson->GetError();

            if ($tResult) {
                //UPDATE PEGAWAI
                $mFunction->SetVariableToMultiFormVariableSupport("idDonatur", $tDonaturId, true, $mIsMultiForm);
                $mFunction->SetVariableToMultiFormVariableSupport("fCurrentPKValue", $tDonaturId, true, $mIsMultiForm);

                $mDataDonatur->SetPrimaryKey('id_donatur', 'fCurrentPKValue', pDonaturId);
                $mDataDonatur->SetField('person_id', 'idPerson', pPersonId);
                $mDataDonatur->SetField('publikasi', 'publikasi', pName, "get");
                $mDataDonatur->SetField('kategori_id', 'kategori', pUnsignedInt, "get");
                $mDataDonatur->SetField('kategori_ket', 'kategoriKet', pLongText, "get");
                $mDataDonatur->SetField('pekerjaan_id', 'pekerjaan', pUnsignedInt, "get");
                $mDataDonatur->SetField('pekerjaan_ket', 'pekerjaanKet', pLongText, "get");
                $mDataDonatur->SetField('media_id', 'media', pUnsignedInt, "get");
                $mDataDonatur->SetField('media_ket', 'mediaKet', pLongText, "get");
                $mDataDonatur->SetField('catatan', 'catatan', pLongText, "get");
                $mDataDonatur->SetField('tanggal_daftar', 'tanggalDaftar', pDateDB, "get");
                if (!$mDataDonatur->GetError()) {
                    //echo "EXECUTE PEGAWAI....ok";
                    $tResult = $mDataDonatur->ExecuteQuery($mAct, false);
                    $_ERRHANDLER = $mDataDonatur->GetError();
                    if ($tResult) {
                        $mDataDonatur->Commit();
                        $mJSONRep->CreateNewData();
                        $mJSONRep->InsertData("Process", "Success");
                        $mJSONRep->CreateNewData();
                        $mJSONRep->InsertData("idPerson", $tPersonId);
                        $mJSONRep->CreateNewData();
                        $mJSONRep->InsertData("idDonatur", $tDonaturId);
                    } else {
                        //INSERT PEGAWAI GAGAL
                        $mJSONRep->SetErrorObjectShowed(true);
                        $mJSONRep->SetErrorSection($_ERRHANDLER[ERROR_SECTION]);
                        $mJSONRep->SetErrorCode($_ERRHANDLER[ERROR_CODE]);
                        $mJSONRep->CreateNewData();
                        $mJSONRep->InsertData("Process", "Failed");
                        $mJSONRep->SetErrorMessage("Update data donatur gagal.");
                    }
                }
            } else {
                //INSERT PEGAWAI GAGAL
                echo "EXECUTE PERSON....failed";
                $mDataPerson->Rollback();
            }
        }
    } else if ($mAct == 'del') {
        $mFunction->SetVariableToMultiFormVariableSupport("idPerson", $tPersonId, false, false);

        $mDataPerson->SetPKValue($tPersonId);
        $mDataPerson->SetPrimaryKey('id_person', 'idPerson', pPersonId);
        $mDataPerson->SetField('id_person', 'idPerson', pPersonId);
        if (!$mDataPerson->GetError()) {
            $tResult = $mDataPerson->ExecuteQuery($mAct);
            $_ERRHANDLER = $mDataPerson->GetError();
            if ($tResult) {
                $mJSONRep->CreateNewData();
                $mJSONRep->InsertData("Process", "Success");
                $mJSONRep->CreateNewData();
                $mJSONRep->InsertData("idPerson", $tPersonId);
            } else {
                $mJSONRep->SetErrorObjectShowed(true);
                $mJSONRep->SetErrorSection($_ERRHANDLER[ERROR_SECTION]);
                $mJSONRep->SetErrorCode($_ERRHANDLER[ERROR_CODE]);
                $mJSONRep->CreateNewData();
                $mJSONRep->InsertData("Process", "Failed");
                $mJSONRep->SetErrorMessage("Destroy data donatur gagal.");
            }
        }
    }
    //STANDAR BAKU, Error Handler
    else {
        $_ERRHANDLER[ERROR_SECTION] = "APPLICATION";
        $_ERRHANDLER[ERROR_CODE] = "EXECUTE_METHOD_NOT_VALID";
    }

    /* =========== *//* =========== *//* =========== *//* =========== *//* =========== *//* =========== */
} else {
    $mJSONRep->SetErrorMessage("Eksekusi Ditolak");
    $mJSONRep->SetErrorSection($_ERRHANDLER[ERROR_Section]);
    $mJSONRep->SetErrorCode($_ERRHANDLER[ERROR_CODE]);
}
print_r($mJSONRep->PrintJSONDataRepresentation());
?>
